//
//  CZVisitorView.swift
//  微博
//
//  Created by 范承志 on 16/6/11.
//  Copyright © 2016年 fanchengzhi. All rights reserved.
//

import UIKit

class CZVisitorView: UIView {

    //定义闭包，监听访客视图的点击回调
    var closure: (()->())?
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //监听按钮点击
    @objc private func loginClick(){
        
        //调用闭包
        closure?()

    }
    
    // MARK: - 供外界调用方法
    /*
     - 因为当前访客视图是参照首页开发的 没有必要再设置
     - 使用可选值 为了开发辨别是要修改哪个界面的HMVisitorView icon 和 messageLabel
     */
    func setupVisitorViewInfo(imgName: String?, tilte: String?){
        // 判断imgName&tilte 是否为 nil
        // 代表就是(消息 发现 我)
        if let img = imgName, let tit = tilte{
            // 设置 image 和 text
            iconImageView.image = UIImage(named: img)
            messageLabel.text = tit
            // 隐藏圆圈
            circleImageView.isHidden = true
        }else {
            // 代表是首页 不需要在设置了
            setupCircleImageViewAnim()
        }
    }
    // 设置圆圈动画
    private func setupCircleImageViewAnim(){
        // 核心动画
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        // 设置转的角度
        anim.toValue = M_PI * 2
        // 设置时间
        anim.duration = 20
        // 重复次数
        anim.repeatCount = MAXFLOAT
        // 完成以后是否移除动画
        /*
         - 如果你切换控制器 或者程序推到后台 默认情况下动画会被移除
         - 解决办法 isRemovedOnCompletion = false
         */
        anim.isRemovedOnCompletion = false
        // 添加动画
        circleImageView.layer.add(anim, forKey: nil)
    }
    
    //搭建界面
    private func setupUI(){
        backgroundColor = UIColor(white: 237/255, alpha: 1)
        //添加控件
        //圆圈
        addSubview(circleImageView)
        //遮罩
        addSubview(maskImageView)
        //房子
        addSubview(iconImageView)
        //文字
        addSubview(messageLabel)
        //注册
        addSubview(registerButton)
        //登录
        addSubview(loginButton)
        
        
        //添加约束
        //圆圈
        circleImageView.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        //遮罩
        maskImageView.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        //房子
        iconImageView.snp_makeConstraints { (make) in
            make.center.equalTo(self)
        }
        //文字
        messageLabel.snp_makeConstraints { (make) in
            make.centerX.equalTo(self)
            make.width.equalTo(230)
            make.top.equalTo(maskImageView.snp_bottom).offset(16)
        }
        //注册
        registerButton.snp_makeConstraints { (make) in
            make.size.equalTo(CGSize(width: 100, height: 35))
            make.left.equalTo(messageLabel)
            make.top.equalTo(messageLabel.snp_bottom).offset(16)
        }
        //登录
        loginButton.snp_makeConstraints { (make) in
            make.size.top.equalTo(registerButton)
            make.right.equalTo(messageLabel)
        }
    }
    
    //懒加载控件
    //圆圈
    fileprivate lazy var circleImageView: UIImageView = UIImageView(imageName: "visitordiscover_feed_image_smallicon")
    //遮罩
    fileprivate lazy var maskImageView: UIImageView = UIImageView(imageName: "visitordiscover_feed_mask_smallicon")
    //房子
    fileprivate lazy var iconImageView: UIImageView = UIImageView(imageName: "visitordiscover_feed_image_house")
    //文字
    fileprivate lazy var messageLabel: UILabel = {
        let lab = UILabel(textColor: UIColor.darkGray, fontSize: CZNormalFontSize, text: "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜")
        //文字对齐
        lab.textAlignment = .center
        //文字换行
        lab.numberOfLines = 0
        return lab
    }()
    //注册按钮
    fileprivate lazy var registerButton: UIButton = {
        let button = UIButton(setBackgroundImgName: "common_button_white", title: "注册", fontSize: CZNormalFontSize, titleColor: CZTHEMECOLOR, target: self, action: #selector(loginClick))
        return button
    }()
    //登录按钮
    fileprivate lazy var loginButton: UIButton = {
        let button = UIButton(setBackgroundImgName: "common_button_white", title: "登录", fontSize: CZNormalFontSize, titleColor: CZTHEMECOLOR, target: self, action: #selector(loginClick))
        return button
    }()
    
    
    
}
